package com.siwei.mes.controller;

import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.mapper.experiment.ExperimentMapper;
import com.siwei.mes.service.system.DevopsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 运维工具接口
 *
 * @Description: 运维工具接口
 * @Version: 1.0
 * @author: mlchen
 * @date: 2025/3/17 19:11
 */
@Api(value = "运维工具接口")
@Slf4j
@RestController
@RequestMapping("devops")
public class DevopsController {
    @Resource
    private DevopsService devopsService;
    @Resource
    private ExperimentMapper experimentMapper;

    /**
     * 作废清理运单
     *
     * @return
     */
    @AnonymousAccess
    @GetMapping("clearExperiment")
    public Json clearExperiment(@RequestParam("experimentNo") String experimentNo) {
        Experiment experiment = experimentMapper.selectByExperimentNo(experimentNo);
        assertTrue(experiment == null, "试验委托不存在，无法清理");

        if (experiment.getExperimentType().equals(MaterialsEnum.CONCRETE.getCode())) {
            assertTrue("该运维接口暂不支持清空混凝土试验委托");
        }
        devopsService.clearExperiment(experiment);
        return Json.succ();
    }

    @AnonymousAccess
    @ApiOperation("导出抗氯离子excel")
    @GetMapping("exportKllzExcel")
    public Json exportKllzExcel(HttpServletRequest request) {
        String filePath = devopsService.exportKllzExcel(request);
        return Json.succ().data(ApiResult.addColumn("filePath", filePath));
    }

    @AnonymousAccess
    @ApiOperation("导入抗氯离子excel")
    @PostMapping("importKllzExcel")
    public Json importKllzExcel(@RequestParam("file") MultipartFile file) {
        Map<String, Object> map = devopsService.importKllzExcel(file);
        return Json.succ().data(map);
    }

    @AnonymousAccess
    @ApiOperation("删除试验对应的任务单或运单关联关系")
    @GetMapping("deleteExperimentRel")
    public Json deleteExperimentRel(@RequestParam("experimentNo") String experimentNo,
                                    @RequestParam(required = false, name = "supplyTaskCode") String supplyTaskCode,
                                    @RequestParam(required = false, name = "waybillId") Long waybillId) {
        if (StringUtils.isBlank(supplyTaskCode) && waybillId == null) {
            assertTrue("任务id或运单id必须传一个");
        }
        devopsService.deleteExperimentRel(experimentNo, supplyTaskCode, waybillId);
        return Json.succ();
    }

    @AnonymousAccess
    @ApiOperation("新增试验对应的任务单或运单关联关系")
    @GetMapping("insertExperimentRel")
    public Json insertExperimentRel(@RequestParam("experimentNo") String experimentNo,
                                    @RequestParam(required = false, name = "supplyTaskCode") String supplyTaskCode,
                                    @RequestParam(required = false, name = "waybillId") Long waybillId) {
        if (StringUtils.isBlank(supplyTaskCode) && waybillId == null) {
            assertTrue("任务id或运单id必须传一个");
        }
        devopsService.insertExperimentRel(experimentNo, supplyTaskCode, waybillId);
        return Json.succ();
    }

    @AnonymousAccess
    @ApiOperation("导出设备excel")
    @GetMapping("exportEquipmentExcel")
    public Json exportEquipmentExcel(HttpServletRequest request) {
        String filePath = devopsService.exportEquipmentExcel(request);
        return Json.succ().data(ApiResult.addColumn("filePath", filePath));
    }

    @AnonymousAccess
    @ApiOperation("导入设备excel")
    @PostMapping("importEquipmentExcel")
    public Json importEquipmentExcel(@RequestParam("file") MultipartFile file) {
        Map<String, Object> map = devopsService.importEquipmentExcel(file);
        return Json.succ().data(map);
    }

    @AnonymousAccess
    @ApiOperation("导出批检配置excel")
    @GetMapping("exportCheckBatchConfigExcel")
    public Json exportCheckBatchConfigExcel(HttpServletRequest request) {
        String filePath = devopsService.exportCheckBatchConfigExcel(request);
        return Json.succ().data(ApiResult.addColumn("filePath", filePath));
    }

    @AnonymousAccess
    @ApiOperation("导入批检配置excel")
    @PostMapping("importCheckBatchConfigExcel")
    public Json importCheckBatchConfigExcel(@RequestParam("file") MultipartFile file) {
        Map<String, Object> map = devopsService.importCheckBatchConfigExcel(file);
        return Json.succ().data(map);
    }

}
